package com.yaodian100.ec.payment.web;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.payment.entity.ApplyClaim;
import com.yaodian100.ec.payment.service.TransferService;

public class ApplyClaimAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = -1402701108549348394L;

	/** logger */
	private final Logger logger = LoggerFactory.getLogger(ApplyClaimAction.class);

	private Menu claimTypeMenu; // 與 getter, setter
	private Menu claimStatusMenu; // 與 getter, setter
	private SimplePager pager;
	private DomainService<Menu> menuService; // 與 setter
	private TransferService transferService;

	private List<ApplyClaim> applyClaims;
	private ApplyClaim applyClaim;
	private String applyClaimId;
	private String imgFileName;
	private String claimReason;
	private String claimStatusResult;

	/* 招领单查询页面参数 */
	private String orderIdParam;
	private String applyClaimIdParam;
	private String claimTypeParam;
	private String claimStatusParam;
	private String startDateParam;
	private String endDateParam;
	private String reviewStartDateParam;
	private String reviewEndDateParam;
	private String chooseDate;

	/**
	 * 招领单收款
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String charge() throws CoreException {
		applyClaim = transferService.getApplyClaim(applyClaimId);
		claimStatusResult = transferService.updateApplyClaimStatus(applyClaim);
		return "jsonChargePayment";
	}

	public ApplyClaim getApplyClaim() {
		return applyClaim;
	}

	public String getApplyClaimId() {
		return applyClaimId;
	}

	public String getApplyClaimIdParam() {
		return applyClaimIdParam;
	}

	public List<ApplyClaim> getApplyClaims() {
		return applyClaims;
	}

	public String getClaimReason() {
		return claimReason;
	}

	public Menu getClaimStatusMenu() {
		return claimStatusMenu;
	}

	public String getClaimStatusParam() {
		return claimStatusParam;
	}

	public String getClaimStatusResult() {
		return claimStatusResult;
	}

	public Menu getClaimTypeMenu() {
		return claimTypeMenu;
	}

	public String getClaimTypeParam() {
		return claimTypeParam;
	}

	public String getEndDateParam() {
		return endDateParam;
	}

	public String getImgFileName() {
		return imgFileName;
	}

	public String getOrderIdParam() {
		return orderIdParam;
	}

	public SimplePager getPager() {
		return pager;
	}

	public String getStartDateParam() {
		return startDateParam;
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (null == pager) {
			pager = new SimplePager();
		}
		pager.setPageRecord(20);// defaut 20

		if (null == claimTypeMenu) {
			claimTypeMenu = menuService.get("ApplyClaim.ClaimType");
		}

		if (null == claimStatusMenu) {
			claimStatusMenu = menuService.get("ApplyClaim.ClaimStatus");
		}

		logger.info("prepare ok");
	}

	/**
	 * 退费作业
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String refund() throws CoreException {
		applyClaim = transferService.getApplyClaim(applyClaimId);
		applyClaim.setClaimStatus("20");
		claimStatusResult = transferService.updateApplyClaimStatus(applyClaim);
		return "jsonRefundPayment";
	}

	/**
	 * 搜寻招领单资料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		try {
			String[] sortOrder = { "id" };
			CommonCriteria crit = new CommonCriteria();
			if (StringUtils.isNotBlank(claimStatusParam)) {
				crit.addEq("claimStatus", claimStatusParam);
			}
			if (StringUtils.isNotBlank(claimTypeParam)) {
				crit.addEq("claimType", claimTypeParam);
			}
			if (StringUtils.isNotBlank(applyClaimIdParam)) {
				crit.addRlike("id", applyClaimIdParam);
			}

			if (StringUtils.isNotBlank(orderIdParam)) {
				crit.addRlike("orderId", orderIdParam);
			}
			if (StringUtils.equalsIgnoreCase("C", chooseDate)) {
				if (StringUtils.isBlank(startDateParam)) {
					startDateParam = EcConstant.PATTERN_DATE_FORMAT.format(DateUtils.addDays(new Date(), -30));
				}

				if (StringUtils.isBlank(endDateParam)) {
					endDateParam = EcConstant.PATTERN_DATE_FORMAT.format(new Date());
				}

				crit.addGe("applyDate", DateUtils.parseDate(startDateParam, new String[] { EcConstant.PATTERN_DATE }));
				crit.addLe("applyDate", DateUtils.parseDate(endDateParam + " 23:59:59",
						new String[] { EcConstant.PATTERN_TIMESTAMP }));
			} else if (StringUtils.equalsIgnoreCase("R", chooseDate)) {
				if (StringUtils.isBlank(reviewStartDateParam)) {
					reviewStartDateParam = EcConstant.PATTERN_DATE_FORMAT.format(DateUtils.addDays(new Date(), -30));
				}

				if (StringUtils.isBlank(reviewEndDateParam)) {
					reviewEndDateParam = EcConstant.PATTERN_DATE_FORMAT.format(new Date());
				}

				crit.addGe("reviewDate", DateUtils.parseDate(reviewStartDateParam, new String[] { EcConstant.PATTERN_DATE }));
				crit.addLe("reviewDate", DateUtils.parseDate(reviewEndDateParam + " 23:59:59",
						new String[] { EcConstant.PATTERN_TIMESTAMP }));
			}

			int applyClaimSize = transferService.getApplyClaimListSize(crit).intValue();
			pager.setTotalSize(applyClaimSize);
			applyClaims = transferService.getApplyClaimList(pager.getCurrentPage() * pager.getPageRecord(), pager
					.getPageRecord(), crit, sortOrder);
		} catch (Exception e) {
			addActionError(e.getMessage());
		}
		return "list";
	}

	public void setApplyClaim(ApplyClaim applyClaim) {
		this.applyClaim = applyClaim;
	}

	public void setApplyClaimId(String applyClaimId) {
		this.applyClaimId = applyClaimId;
	}

	public void setApplyClaimIdParam(String applyClaimIdParam) {
		this.applyClaimIdParam = applyClaimIdParam;
	}

	public void setApplyClaims(List<ApplyClaim> applyClaims) {
		this.applyClaims = applyClaims;
	}

	public void setClaimReason(String claimReason) {
		this.claimReason = claimReason;
	}

	public void setClaimStatusMenu(Menu claimStatusMenu) {
		this.claimStatusMenu = claimStatusMenu;
	}

	public void setClaimStatusParam(String claimStatusParam) {
		this.claimStatusParam = claimStatusParam;
	}

	public void setClaimStatusResult(String claimStatusResult) {
		this.claimStatusResult = claimStatusResult;
	}

	public void setClaimTypeMenu(Menu claimTypeMenu) {
		this.claimTypeMenu = claimTypeMenu;
	}

	public void setClaimTypeParam(String claimTypeParam) {
		this.claimTypeParam = claimTypeParam;
	}

	public void setEndDateParam(String endDateParam) {
		this.endDateParam = endDateParam;
	}

	public void setImgFileName(String imgFileName) {
		this.imgFileName = imgFileName;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setOrderIdParam(String orderIdParam) {
		this.orderIdParam = orderIdParam;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public void setStartDateParam(String startDateParam) {
		this.startDateParam = startDateParam;
	}

	public void setTransferService(TransferService transferService) {
		this.transferService = transferService;
	}

	/**
	 * 退件
	 * 
	 * @return
	 * @throws CoreException
	 */
	public String updateReason() throws CoreException {
		applyClaim = transferService.getApplyClaim(applyClaimId);
		applyClaim.setClaimStatus("90");
		applyClaim.setClaimReason(claimReason);
		transferService.updateApplyClaimStatus(applyClaim);
		claimStatusResult = applyClaim.getClaimStatus();
		return "updateReason";
	}

	public String view() throws CoreException {
		if (StringUtils.isNotBlank(applyClaimId)) {
			applyClaim = transferService.getApplyClaim(applyClaimId);
			if (new File(ServerValue.getMidApachePath() + "claimImg").exists()) {
				File file = new File(ServerValue.getMidApachePath() + "claimImg/");
				// 因为不知檔案附檔名为何，所以要 依招领单号去搜寻
				File[] files = file.listFiles(new FilenameFilter() {

					public boolean accept(File dir, String name) {
						return (name.contains(applyClaimId));
					}
				});
				if (files.length > 0) {
					imgFileName = files[0].getName();
				}
			}
		}
		return "view";
	}

	public String getReviewStartDateParam() {
		return reviewStartDateParam;
	}

	public String getReviewEndDateParam() {
		return reviewEndDateParam;
	}

	public String getChooseDate() {
		return chooseDate;
	}

	public void setReviewStartDateParam(String reviewStartDateParam) {
		this.reviewStartDateParam = reviewStartDateParam;
	}

	public void setReviewEndDateParam(String reviewEndDateParam) {
		this.reviewEndDateParam = reviewEndDateParam;
	}

	public void setChooseDate(String chooseDate) {
		this.chooseDate = chooseDate;
	}
}